package com.hwd.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hwd.gmall.model.product.BaseCategory1;
import com.hwd.gmall.product.service.BaseCategory1Service;
import org.springframework.web.bind.annotation.*;
import com.hwd.gmall.common.result.Result;

import javax.annotation.Resource;
import java.util.List;

/**
 * 一级分类的 controller 层
 *
 * @author 黄伟东/Victor
 * @date 2022/4/17 18:58
 */
@RestController
@RequestMapping(value = "/api/category1")
public class BaseCategory1Controller {

    @Resource
    private BaseCategory1Service baseCategory1Service;


    /**
     * 根据id获取一级分类表信息
     *
     * @param id 一级分类id
     * @return com.hwd.gmall.common.result.Result<com.hwd.gmall.model.product.BaseCategory1>
     */
    @GetMapping(value = "/getCategory1/{id}")
    public Result<BaseCategory1> getCategory1ById(@PathVariable(value = "id") Long id) {
        return Result.ok(baseCategory1Service.getById(id));
    }

    /**
     * 查询全部一节分类
     *
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseCategory1>>
     */
    @GetMapping(value = "/listCategory1s")
    public Result<List<BaseCategory1>> listCategory1s() {
        return Result.ok(baseCategory1Service.listCategory1s());
    }

    /**
     * 新增一级分类数据
     *
     * @param baseCategory1 一级分类数据
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @PostMapping(value = "/saveCategory1")
    public Result<Object> saveCategory1(@RequestBody BaseCategory1 baseCategory1) {
        baseCategory1Service.saveCategory1(baseCategory1);
        return Result.ok();
    }

    /**
     * 根据id删除一级分类数据
     *
     * @param id 一级分类id
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @DeleteMapping(value = "/deleteCategory1/{id}")
    public Result<Object> deleteCategory1(@PathVariable(value = "id") Long id) {
        baseCategory1Service.deleteCategory1(id);
        return Result.ok();
    }

    /**
     * 修改一级分类数据
     *
     * @param baseCategory1 一级分类数据
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @PutMapping(value = "updateCategory1")
    public Result<Object> updateCategory1(@RequestBody BaseCategory1 baseCategory1) {
        baseCategory1Service.updateCategory1(baseCategory1);
        return Result.ok();
    }

    /**
     * 分页查询
     *
     * @param page 当前页数
     * @param size 每页显示数量
     * @return com.hwd.gmall.common.result.Result<com.baomidou.mybatisplus.core.metadata.IPage < com.hwd.gmall.model.product.BaseCategory1>>
     */
    @GetMapping(value = "/page/{page}/{size}")
    public Result<IPage<BaseCategory1>> page(@PathVariable(value = "page") Integer page, @PathVariable(value = "size") Integer size) {
        return Result.ok(baseCategory1Service.page(page, size));
    }

    /**
     * 条件查询
     *
     * @param baseCategory1 查询条件
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseCategory1>>
     */
    @GetMapping(value = "/search")
    public Result<List<BaseCategory1>> search(@RequestBody BaseCategory1 baseCategory1) {
        return Result.ok(baseCategory1Service.search(baseCategory1));
    }

    /**
     * 分页条件查询
     *
     * @param baseCategory1 查询条件
     * @param page          当前页数
     * @param size          每页显示数量
     * @return com.hwd.gmall.common.result.Result<com.baomidou.mybatisplus.core.metadata.IPage < com.hwd.gmall.model.product.BaseCategory1>>
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<IPage<BaseCategory1>> search(@RequestBody BaseCategory1 baseCategory1,
                                               @PathVariable(value = "page") Integer page,
                                               @PathVariable(value = "size") Integer size) {
        return Result.ok(baseCategory1Service.search(baseCategory1, page, size));
    }

}
